package com.aaa.controller;

import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet(name = "LoginServlet",urlPatterns = "/alogin.action")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username=request.getParameter("username");
        String pwd=request.getParameter("pwd");
        User user=new User(username,pwd);
        UserService service=new UserServiceImpl();

        Map<String,Object> map=service.login(user);
        PrintWriter writer = response.getWriter();
        if (map!=null)
        {
            request.getSession().setAttribute("user",map.get("user_id"));
            writer.print("ok");
        }
        else {
            writer.print("用户名或者密码错误");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
